package com.xbn.xbnmall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XBN
 * @date 2020/8/16 15:25
 * @email 78742541@qq.com
 */
@Configuration
public class MyRabbitConfig {
    @Autowired
    RabbitTemplate rabbitTemplate;


    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange stockEventExchange(){
        TopicExchange topicExchange = new TopicExchange("stock-event-exchange",
                true, false);
        return topicExchange;
    }
    @Bean
    public Queue stockReleaseStockQueue(){
        Queue queue = new Queue("stock.release.stock.queue", true,
                false, false);

        return queue;
    }

    /*
     * 死信队列
     * */
    @Bean
    public Queue stockDelayQueue(){
        Map<String,Object> arguments=new HashMap<>();
        //死信交换机，消息死亡后转发至某个交换机
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        //路由
        arguments.put("x-dead-letter-routing-key","stock.release");
        //消息死亡时间，单位ms
        arguments.put("x-message-ttl",1000*60);
        Queue queue = new Queue("stock.delay.queue", true,
                false, false,arguments);

        return queue;
    }

    @Bean
    public Binding stockReleaseBinding(){
        Binding binding = new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",null);
        return binding;
    }
    @Bean
    public Binding stockLockedBinding(){
        Binding binding = new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked.#",null);
        return binding;
    }
    //构造器执行对象创建之后调用这个方法
    //只要消息代理收到了消息，这个方法就会执行
/*    @PostConstruct
    public void initRabbitTemplate(){
        //设置消息抵达broker消息服务的确认回调
        rabbitTemplate
                .setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
                    @Override
                    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                        System.out.println("消息确认...成功发送给broker消息服务器  correlationData"
                        +":"+correlationData+"ack"+ack+"cause"+cause);
                    }
                });
        //设置消息抵达队列的确认回调，只要消息没有投递给指定的队列就触发这个失败回调
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText,
                                        String exchange, String routingKey) {
                System.out.println("消息投递队列失败，消息信息："+message+"回复文本："+replyText
                +"：投递的交换器"+exchange+",路由键"+routingKey);
            }
        });
    }*/
}
